//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#include <buola/buola.h>

#include <buola/app/ctimer.h>
#include <buola/app/ceventloop.h>

namespace buola {

CTimer::CTimer(const chrono::microseconds &pInterval,bool pActive,EFlags pFlags)
    :   mInterval(pInterval)
    ,   mActive(false)
    ,   mFlags(pFlags)
    ,   mDeleted(nullptr)
    ,   mEventLoop(get_main_event_loop())
{
    if(pActive) Enable();

    mEventLoop.RegisterTimer(this);
}

CTimer::CTimer(CEventLoop& pEventLoop,const chrono::microseconds &pInterval,bool pActive,EFlags pFlags)
    :   mInterval(pInterval)
    ,   mActive(false)
    ,   mFlags(pFlags)
    ,   mDeleted(nullptr)
    ,   mEventLoop(pEventLoop)
{
    if(pActive) Enable();
    
    mEventLoop.RegisterTimer(this);
}

CTimer::~CTimer()
{
    mEventLoop.UnregisterTimer(this);
    if(mDeleted)
        *mDeleted=true;
}

#if BUOLA_PLATFORM_LINUX
void CTimer::SetAbsTime(const CCalendarTime &pTime)
{
    SetInterval(pTime-CCalendarClock::now(),true);
}
#endif //BUOLA_PLATFORM_LINUX

void CTimer::SetAbsTime(const CMonotonicTime &pTime)
{
    mNext=pTime;

    mActive=true;

    mEventLoop.UpdateTimer(this);

    mFlags|=FLAG_ONCE;
}

void CTimer::SetFlags(EFlags pFlags)
{
    mFlags=pFlags;
}

void CTimer::Enable()
{
    mActive=true;

    mNext=CMonotonicClock::now()+mInterval;

    mEventLoop.UpdateTimer(this);
}

void CTimer::Disable()
{
    mActive=false;
}

/*namespace buola*/ }
